import { _decorator, Component, Node, error, resources, SpriteFrame, Texture2D, Prefab, AssetManager, assetManager } from 'cc';
import { BundleManager } from './bundle_Manager';
const { ccclass, property } = _decorator;

@ccclass('ResManager')
export class ResManager {
    private static _instance: ResManager;
    public static get instance () {
        if (this._instance) {
            return this._instance;
        }

        this._instance = new ResManager();
        return this._instance;
    }

    /**
     * 通过uuid加载bundle包资源
     * @param uuid 
     * @returns 
     */
    public async loadBundleResByUUID (uuid: string) {
        const bundle = await BundleManager.instance.getBundleByUUID(uuid) as AssetManager.Bundle;

        const assetInfo = bundle.getAssetInfo(uuid);

        if (!assetInfo) {
            console.error('未找到')
            return
        }

        return new Promise((resolve, reject) => {
            //@ts-ignore
            bundle.load(assetInfo.path, assetInfo.ctor, (err, res: any) => {
                if (err) {
                    error(err.message || err);
                    reject && reject(err)
                    return;
                }

                resolve && resolve(res);
            })
        })
    }
    /**
      * 通过uuid加载释放包资源
      * @param uuid 
      * @returns 
      */
    public async releaseBundleResByUUID (uuid: string) {
        const bundle = await BundleManager.instance.getBundleByUUID(uuid) as AssetManager.Bundle;

        const assetInfo = bundle.getAssetInfo(uuid);

        if (!assetInfo) {
            console.error('未找到')
            return
        }
        //@ts-ignore
        bundle.release(assetInfo.path, assetInfo.ctor);
    }


    /**
     * 通过地址或者名字加载资源
     * @param urlOrName 
     * @param bundleName 
     * @param type 
     */
    public async loadBundleResByUrlOrName (urlOrName: string, bundleName: string, type: any) {
        const bundle = await BundleManager.instance.getBundle(bundleName) as AssetManager.Bundle;

        return new Promise((resolve, reject) => {
            bundle.load(urlOrName, type, (err: any, res: any) => {
                if (err) {
                    error(err.message || err);
                    return;
                }
                resolve && resolve(res);
            })

        })
    }


    /**
     * 通过地址或者名字加载文件夹下资源
     * @param urlOrName 
     * @param bundleName 
     * @param type 
     */
    public async loadBundleDirResByUrlOrName (urlOrName: string, bundleName: string, type: any) {
        const bundle = await BundleManager.instance.getBundle(bundleName) as AssetManager.Bundle;

        return new Promise((resolve, reject) => {
            bundle.loadDir(urlOrName, type, (err: any, res: any) => {
                if (err) {
                    error(err.message || err);
                    return;
                }
                resolve && resolve(res);
            })

        })
    }



    /**
   * 通过地址或者名字加载资源
   * @param urlOrName 
   * @param bundleName 
   * @param type 
   */
    public async releaseBundleResByUrlOrName (urlOrName: string, bundleName: string, type: any) {
        const bundle = await BundleManager.instance.getBundle(bundleName) as AssetManager.Bundle;

        bundle.release(urlOrName, type);
    }
}

